<template>
	<view class="add page bgf7 p12">
		<view class="bgff p22">
			<view v-for="(item,index) in list" :key="index" class="flex flexC mb16">
				<view class="flex flexC flex1">
					<text class="ml8">充值</text>
					<input v-model="item.amount" type="digit" class="flex1 bor r40 px12 py6" placeholder="充100元" />
				</view>
				<view class="flex flexC flex1 ">
					<text class="ml8">得</text>
					<input type="digit" v-model="item.get_amount" class="flex1 bor r40 px12 py6" placeholder="得180元" />
					<image @click="delItem(item)" class="w45" src="../../static/img/icon/del_icon.png"></image>
				</view>
			</view>
			<view class="flex flexC aliC mt36">
				<view class="mr30 flex1 c00 bgf6 r40 py12" @click="addItem">添加数据项</view>
				<view class="flex1 bg41 r40 py12" @click="save">保存数据</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

				selectShop: null,
				list: []
			};
		},
		onLoad(option) {
			var shop  = option.selectShop;
			if(shop != ''){
				this.selectShop = JSON.parse(shop);
			}
		
			this.getConfigList();
		},

		methods: {


			getConfigList() {
				let param = {
					shop_id: this.selectShop.id,

				};
				let that = this;
				this.$api.configList(param).then(res => {

					if (res.code == 1) {
						this.list = res.data;
					} else {

					}
				})
			},


			delItem(item) {
				let param = {
					id: item.id,

				};
				let that = this;
				this.$api.removeConfigItem(param).then(res => {

					if (res.code == 1) {
						that.getConfigList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})


			},

			addItem() {

				var obj = new Object();
				obj.id = -1;
				obj.amount = "";
				obj.get_amount = "",
				this.list.push(obj);
			},
			
			save(){
				var isOk = true;
				for(var i=0;i<this.list.length;i++){
					
					if(this.list[i].amount == "" || this.list[i].get_amount == ""){
						isOk = false;
					}
				}
				
				if(!isOk){
					uni.showToast({
						title: "价格不能为0",
						icon: 'none'
					})
					return;
				}
				let param = {
					id: this.selectShop.id,
					set:JSON.stringify(this.list)	
				};
				let that = this;
				
				this.$api.addConfigSet(param).then(res => {
				
					if (res.code == 1) {
						uni.showModal({
							title:"添加提示",
							content:"添加成功",
							showCancel:false,
							complete() {
								uni.navigateBack({
									delta:1
								})
							}
						})
						
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
				
			}
		}

	}
</script>

<style lang="scss">
	.w45 {
		width: 60rpx;
		height: 60rpx;
	}
</style>
